﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using CM.DL.DALC.NC03;
using DECOR.BL.BE;

namespace DECOR.DL.DALC.NC03
{
    public class CondicionPagoDALC : ICondicionPagoDALC
    {
        private string _connId = string.Empty;

        public static CondicionPagoDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(CondicionPagoDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new CondicionPagoDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private CondicionPagoDALC(string pConnId) { this._connId = pConnId; }
        private CondicionPagoDALC() { }
        private static volatile CondicionPagoDALC _instance = null;

        #region ICondicionPagoDALC Members

        public List<DECOR.BL.BE.GenericBE> listar(string pID)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            GenericBE obj = null;
            List<GenericBE> listaResult = new List<GenericBE>();
            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }
                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_CONDICIONESPAGO");
                function.SetValue("IM_ZTERM", pID);
                function.Invoke(dest);

                IRfcTable T052U = function.GetTable("T_T052U");

                foreach (IRfcStructure item in T052U)
                {
                    obj = new GenericBE();

                    obj.Codigo = item["ZTERM"].GetString();
                    obj.Descripcion = item["ZTERM"].GetString() + " :: " + item["TEXT1"].GetString();
                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        #endregion
    }
}
